package stock

import (
	"context"
	"fmt"
	"gitee.com/bei_yi_ning/gorder/stock/entity"
	"strings"
)

type Repository interface {
	GetItems(ctx context.Context, ids []string) ([]*entity.Item, error)
	GetStock(ctx context.Context, ids []string) ([]*entity.ItemWithQuantity, error)
	UpdateStock(ctx context.Context, query []*entity.ItemWithQuantity, f func(ctx context.Context, existing []*entity.ItemWithQuantity, query []*entity.ItemWithQuantity) ([]*entity.ItemWithQuantity, error)) error
}

type NotFoundError struct {
	MissingIDs []string
}

func (e NotFoundError) Error() string {
	return fmt.Sprintf("the following items are not found: %s", strings.Join(e.MissingIDs, ","))
}

type ExceededStockError struct {
	FailedOn []struct {
		ProductID string
		Want      int32
		Have      int32
	}
}

func (e ExceededStockError) Error() string {
	retErr := ""
	for _, errInfo := range e.FailedOn {
		retErr += fmt.Sprintf("items id: %s exceeded , want %d, now we have %d \n", errInfo.ProductID, errInfo.Want, errInfo.Have)
	}
	return retErr
}
